package com.example.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.admin.common.result.R;
import com.example.admin.entity.Configuration;
import com.example.admin.model.dto.ConfigurationRequest;
import com.example.admin.service.ConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "系统配置接口")
@RestController
@RequestMapping("/api/config")
public class ConfigurationController {

    @Autowired
    private ConfigurationService configurationService;

    @ApiOperation("分页查询配置列表")
    @PostMapping("/list")
    public R<IPage<Configuration>> list(@RequestBody ConfigurationRequest request) {
        return R.ok(configurationService.getConfigPage(request));
    }

    @ApiOperation("获取配置详情")
    @GetMapping("/get")
    public R<Configuration> getById(@RequestParam Long id) {
        return R.ok(configurationService.getById(id));
    }

    @ApiOperation("根据类型和名称获取配置")
    @GetMapping("/getByTypeAndName")
    public R<Configuration> getByTypeAndName(
            @RequestParam String type,
            @RequestParam String name,
            @RequestParam(defaultValue = "system") String userId) {
        return R.ok(configurationService.getByTypeAndName(type, name, userId));
    }

    @ApiOperation("保存或更新配置")
    @PostMapping("/save")
    public R<Void> save(@RequestBody Configuration configuration) {
        configurationService.saveOrUpdate(configuration);
        return R.ok();
    }

    @ApiOperation("删除配置")
    @GetMapping("/delete")
    public R<Void> delete(@RequestParam Long id) {
        configurationService.deleteById(id);
        return R.ok();
    }
} 